package com.pda.yuanjing.pages.tpzz;

import android.content.Context;

import com.apkfuns.logutils.LogUtils;
import com.pda.yuanjing.beanin.JieDianBeanIn;
import com.pda.yuanjing.beanout.NoBillBeanOut;
import com.pda.yuanjing.beanout.TPBeanOut;
import com.pda.yuanjing.thread.CodeBeans;
import com.public_module.basemvp.model.BaseModel;
import com.public_module.config.SetConfig;
import com.public_module.tools.SharedPreferencesManger;
import com.public_module.tools.StringProcess;

import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

public class ScanfModelImpl extends BaseModel implements ScanfModel {
    private String TAG = "ScanfModelImpl";

    public ScanfModelImpl(Context context) {
        super(context);
    }

    @Override
    public void scanfCodenobill(String  tuoCode,  String code ,CodeBeans codeBeans, CallBack callBack)
    {
        TPBeanOut tpBeanOut = new TPBeanOut();
        tpBeanOut.setErrcode(-1);
        tpBeanOut.setErrmsg(SetConfig.getConnectError());
        tpBeanOut.setData(null);
        try {
            JieDianBeanIn jieDianBeanIn = new JieDianBeanIn();
            jieDianBeanIn.setAction("SY_UpdateCode_TuoPanZuZhuang");
            jieDianBeanIn.setCid(SharedPreferencesManger.getCid());
            jieDianBeanIn.setUserID(SharedPreferencesManger.getUid());
            jieDianBeanIn.setStoreID(SharedPreferencesManger.getStoreID());
            jieDianBeanIn.setTuoCode(tuoCode);
            jieDianBeanIn.setCodecc(code);

            RequestBody body = RequestBody.create(SetConfig.JSON, gson.toJson(jieDianBeanIn));
            final Call<ResponseBody> call = apisync.addCode(body);
            Response<ResponseBody> response = call.execute();
            if (response.isSuccessful()) {
                LogUtils.e(StringProcess.responsetoString(response.body()));
                tpBeanOut = gson.fromJson(StringProcess.responsetoString(response.body()), TPBeanOut.class);
                tpBeanOut.setErrcode(tpBeanOut.getErrcode());
                tpBeanOut.setErrmsg(tpBeanOut.getErrmsg());
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            codeBeans.removeCode(codeBeans.getCodes().get(0));
            callBack.onshowSuccessresNoBill(tpBeanOut);
        }
    }
}

